package fragments;

import info.androidhive.slidingmenu.R;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.util.Log;
import android.view.MenuItem;
import android.widget.ListView;
import mindshare.LoadContent;

public class Post {
	private String text;
	private int postId;
	private String name;
	private String postType;
	private String category;
	private String parentId;
	MenuItem refreshMenuItem;
	ListView lv;
	Context cont;
	List<NameValuePair> params = new ArrayList<NameValuePair>();

	public Post(int id, Context c, MenuItem m, ListView l) {
		this.postId = id;
		this.cont = c;
		this.refreshMenuItem = m;
		this.lv=l;
		obtainPostInfo();
	}

	// Context cont;
	/*
	 * public Post(Context c){ this.cont = c; }
	 */

	public class GetPostInfo extends LoadContent {
		// Hacer php que saque el nombre de usuario,texto del post, tipo,
		// categoria, idpost padr

		private MenuItem refreshMenuItem;

		public GetPostInfo(Context context, List<NameValuePair> param, String data, MenuItem m) {
			super(context, param, data);
			this.refreshMenuItem=m;
			Log.d("TAG", "ROBERTO");
		}

		@Override
		protected void onPreExecute() {
			refreshMenuItem.setActionView(R.layout.action_progressbar);
			refreshMenuItem.expandActionView();
		}

		@Override
		protected String doInBackground(String... args) {
			JSONObject json = jParser.makeHttpRequest("http://mindshare.comli.com/getPostInfo.php", param);
			JSONArray postInfo;
			try {
				postInfo = json.getJSONArray("postInfoArray");
				/*
				 * for (int i = 0; i < postInfo.length(); i++) { String c =
				 * postInfo.getString(i); contentList.add(c); }
				 */
				name = postInfo.getString(0);
				text = postInfo.getString(1);
				postType = postInfo.getString(2);
				category = postInfo.getString(3);
				parentId = postInfo.getString(4);
				Log.d("TAG", "POST name:" + name);
				Log.d("TAG", "POST category:" + category);

			} catch (JSONException e) {
				e.printStackTrace();
			} catch (NullPointerException e) {
				e.printStackTrace();
			}

			return null;
		}

		protected void onPostExecute(String file_url) {
			lv.invalidateViews();
			refreshMenuItem.collapseActionView();
			refreshMenuItem.setActionView(null);
		}

	}

	public String getCategory() {
		return category;
	}

	public String getText() {
		return text;
	}

	public String getName() {
		return name;
	}

	public int getPostId() {
		return postId;
	}

	public void obtainPostInfo() {
		params.clear();
		params.add(new BasicNameValuePair("postId", postId + ""));
		// guardar postId como parametro para la conexion y hacer la conexion
		// creando un GetPostInfo y ejecut�ndolo

		GetPostInfo gPI = new GetPostInfo(cont, params, "", refreshMenuItem); // SI NO FUNCIONA
																// ES PROBABLE
																// QUE SEA
																// PORQUE EL
																// POST USA UN
																// INT Y ESTOY
																// ENVANDO UN
																// STR
		Log.d("DEB", "Debuggin:" + params);
		gPI.execute();

	}
}
